<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- <form action="http://www.baidu.com" onsubmit="return false"> -->
    <!-- <form action="http://www.baidu.com"> -->
        账号: <input type="text" name="" id=""><br>
        密码: <input type="password" name="" id=""><br>
        <input type="submit" value="登录">        
    <!-- </form> -->
    <script>
        var form = document.querySelector("form")
        var username = document.querySelector("input[type=text]")
        var password = document.querySelector("input[type=password]")
        //判断输入账号的时候回车键进行登录
        username.onkeydown = function(e){
            if(e.keyCode == 13){
                login();
            }
        }
        //判断输入密码的时候按回车键进行登录
        password.onkeydown = function(e){
            if(e.keyCode == 13){
                login();
            }
        }

        //登录
        function login(){
            if(username.value.trim().length > 0 && password.value.trim().length > 0){
                alert("登录!")
            }else{
                alert("账号密码不能为空!")
                //阻止表单的默认提交
                return false;
            }
        }
    </script>
</body>
</html>